package studySpringBoot.com.springboot;

import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.CompletableFuture;

/**
 *
 *
 */
public class ZhiGanImportSelector implements DeferredImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        /**
         * SPI机制--ServiceLoader
         */
        ServiceLoader<AutoConfiguration> serviceLoader = ServiceLoader.load(AutoConfiguration.class);
        List<String> list = new ArrayList<>();
        for (AutoConfiguration autoConfiguration : serviceLoader) {
            list.add(autoConfiguration.getClass().getName());
        }
        return list.toArray(new String[0]);
    }


    public static void main(String[] args) {
        test();
    }


    public static void test() {
        CompletableFuture<String> root = new CompletableFuture<>();
        root.thenApply(s -> {
            System.out.println("3");
            return "3";
        });
        root.thenApply(s -> {
            System.out.println("6");
            return "6";
        });
        root.thenApply(s -> {
            System.out.println("9");
            return "9";
        });
        root.complete("1");

        System.out.println("=============================");
        CompletableFuture<String> root1 = new CompletableFuture<>();
        root1.thenApply(s -> {
            return "2";
        });
        CompletableFuture bcf = root1.thenApply(s -> {
            return "7";
        });
        root1.thenApply(s -> {
            return "4";
        });

        bcf.thenApply(s -> {
            System.out.println("3");
            return "3";
        });
        bcf.thenApply(s -> {
            System.out.println("6");
            return "6";
        });
        bcf.thenApply(s -> {
            System.out.println("9");
            return "9";
        });
        root1.complete("1");
    }


}
